package com.service.impl;

import com.dao.UserDao;
import com.entity.ResponseEntity;
import com.entity.User;
import com.github.pagehelper.PageHelper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public ResponseEntity findOne(User user) {
        User _user = userDao.findOne(user);
        if(_user != null){
            return new ResponseEntity(200,"已找到",user);
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity findAll(int pageNum) {
        PageHelper.startPage(pageNum,5);
        List<User> list = userDao.findAll();
        if(list != null){

            return new ResponseEntity<List<User>>(200,"成功找到",list);
        }else {
            return new ResponseEntity(500,"用户表为空",null);
        }
    }

    @Override
    public ResponseEntity findName(String username) {
        User user = userDao.findName(username);
        if(user == null){
            return new ResponseEntity(200,"用户名可用",true);
        }else {
            return new ResponseEntity(500,"用户名已存在",false);

        }
    }

    @Override
    public ResponseEntity addOne(User user) {
        String code = UUID.randomUUID().toString().replace("-", "");
        user.setCode(code);
        user.setStatus(false);
        int i = userDao.addOne(user);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.FAIL;
        }
    }
}
